// linepre.js
var app = getApp();
var bmap = require('../../../common/bmap-wx.min.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    distance: "约34km",
    up: true,
    down: true,
    upSite: '',
    downSite: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.showLoadings();
    var that = this;

    // 百度地图定位获取当前位置信息
    var BMap = new bmap.BMapWX({
      ak: 'D097238894b7c9f3ddf8fd92be560188'
    });
    var success = function (data) {
      var lat = data.originalData.result.location.lat,
        lng = data.originalData.result.location.lng;
      that.setData({
        lat: data.originalData.result.location.lat,
        lng: data.originalData.result.location.lng
      })
      // 获取后台数据
      console.log(data)
      that.vehiclePosition(lat, lng)
    }
    var fail = function (data) {
      console.log(data);
    }
    BMap.regeocoding({
      fail: fail,
      success: success
    });
  },

  refresh: function (e) {
    app.showLoadings();
    this.vehiclePosition();
  },

  // 车辆信息、位置
  vehiclePosition: function (lat, lng) {
    var that = this;
    wx.request({
      url: app.serviceurl + 'CustomLine/CustomBusLocation',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {
        BusLineID: app.line.id,
        Latitude: lat || this.data.lat,
        Longitude: lng || this.data.lng
      },
      dataType: 'json',
      success: function (res) {
        app.hideLoadings();
        if(res.data.Message != ''){
          that.AlertName(res.data.Message)
        }
        that.setData({
          data: res.data
        });

        if (res.data.TakePoints != null && typeof (res.data.TakePoints) != undefined) {

          var IsUpPoint = res.data.TakePoints,
            Up = [],
            Down = [];
          IsUpPoint.forEach(function (e) {
            if (e.IsUpPoint) {
              Up.push(e)
            } else {
              Down.push(e)
            }
          })
          that.setData({
            up: Up,
            down: Down
          })
        }
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },

  location: function (e) {
    var stand = e.currentTarget.dataset.stand,
      latitude = Number(e.currentTarget.dataset.location),
      longitude = Number(e.currentTarget.dataset.longitude);
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 14,
      name: stand,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return app.OnShare();
    }
  },

  // 提示框
  AlertName: function (message) {
    wx.showModal({
      content: message,
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          wx.navigateBack({})
        }
      }
    });
  },
})